package org.lilred.nzbberry.webservice;

import org.json.me.JSONException;
import org.json.me.JSONObject;
import org.lilred.nzbberry.http.HttpRequestDispatcher;

public class SabnzbdOutputClient extends SabnzbdClient {
	RequestsOutput requester;
	String mode;
	
	public SabnzbdOutputClient(String mode, RequestsOutput requester) {
		this.mode = mode;
		this.requester = requester;
		dispatcher = new HttpRequestDispatcher(getApiUrl() + "&mode=" + mode, "GET", this);
	}
	
	public void start() {
		dispatcher.start();
	}
	
	public void requestSucceeded(byte[] result, String contentType) {
		JSONObject jobj = new JSONObject();
		try {
			jobj = new JSONObject(new String(result));
		} catch (JSONException e) {
			System.out.println(e.getMessage());
		}
		requester.webserviceOutputReceived(jobj);
	}
	
	public void requestFailed(byte[] result, int responseCode) {
		System.out.println("HTTP Request Failed.  Response code: " + responseCode + ". Output: ");
		System.out.println(new String(result));
		requester.webserviceRequestFailed();
	}
}
